 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"handler.h"
#include "HandlerThread.h"
#include <unistd.h>
#include <utils/Looper.h>

using namespace android;

class Fujica_Handler;

typedef  void (*handler_callback)(int,int,int,const char *);

typedef struct handler_{
  Fujica_Handler *handler;
  HandlerThread *mHandlerThread;
}handler_t;

class Fujica_Handler : public MessageHandler
   {
	private:
    	handler_callback cb;
    public:
        virtual void handleMessage(const Message &message);
        void set_cb(handler_callback cb){this->cb=cb;}
};

void Fujica_Handler::handleMessage(const Message &message)
{

	if(this->cb){
		this->cb(message.what,message.arg1,message.arg2,message.data);
	}
}

void send_EmptyMessage_Delay(void *handle,int what,int arg1,int arg2,long uptimeMillis)
{
    if(handle)
    {
       	handler_t *hadler=(handler_t *)handle;
        sp<Looper> threadLooper =hadler->mHandlerThread->getLooper();
		if(threadLooper!=NULL)
        threadLooper->sendMessageDelayed(ms2ns(uptimeMillis), hadler->handler, Message(what,arg1,arg2));
    }
}

void send_EmptyMessage(void *handle,int what,int arg1,int arg2)
{
    if(handle)
    {
		handler_t *hadler=(handler_t *)handle;
        sp<Looper> threadLooper =hadler->mHandlerThread->getLooper();
		if(threadLooper!=NULL){
             threadLooper->sendMessage(hadler->handler, Message(what,arg1,arg2));
		}
    }
}

void send_Message_Delay(void *handle,int what,int arg1,char *p,long uptimeMillis){

   if(handle)
    {
		handler_t *hadler=(handler_t *)handle;
        sp<Looper> threadLooper =hadler->mHandlerThread->getLooper();
		if(threadLooper!=NULL)
        threadLooper->sendMessageDelayed(ms2ns(uptimeMillis),hadler->handler, Message(what,arg1,p));
    }
	
}

void send_Message(void *handle,int what,int arg1,char *p){
	
	if(handle)
    {
		handler_t *hadler=(handler_t *)handle;
        sp<Looper> threadLooper =hadler->mHandlerThread->getLooper();
		if(threadLooper!=NULL)
        threadLooper->sendMessage(hadler->handler, Message(what,arg1,p));
    }
}

void remove_Messages(void *handle,int what)
{
    if(handle)
    {
        handler_t *hadler=(handler_t *)handle;
        sp<Looper> threadLooper =hadler->mHandlerThread->getLooper();
		if(threadLooper!=NULL)
        threadLooper->removeMessages(hadler->handler,what);
    }
}
void remove_ALL_Messages(void *handle)
{
    if(handle)
    {
        handler_t *hadler=(handler_t *)handle;
        sp<Looper> threadLooper =hadler->mHandlerThread->getLooper();
		if(threadLooper!=NULL)
             threadLooper->removeMessages(hadler->handler);
    }
}


int  handler_init(void **handle,void (*cb)(int,int,int,const char *))
{
	if(*handle==NULL){
	
	  handler_t *hadler=(handler_t *)malloc(sizeof(handler_t));
	  if(hadler==NULL){
		return -1;
	  }
	  memset(hadler,0,sizeof(handler_t));
	  hadler->handler=new Fujica_Handler();
	  hadler->mHandlerThread = new HandlerThread();
      hadler->mHandlerThread->start("VMS.NATIVE_LOOP");
      hadler->handler->set_cb((handler_callback)cb);
	  *handle=hadler;
	}
    return 1;
}

int handler_deinit(void *handler)
{
    if(handler!=NULL)
    {
		handler_t *hadler=(handler_t *)handler;
        hadler->mHandlerThread->quit();
		free(handler);
        return true;
    }
    return false;

}


